package com.keyancompany.zhongbaopingtai.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

public class EditUserInfoActivity extends BaseActivity {
    public static final String INTENT_TYPE = "TYPE";
    public static final String INTENT_VALUE = "VALUE";

    public static final String INTENT_USER_NICK = "USER_NICK";
    public static final String INTENT_USER_INTRO = "USER_INTRO";


    @BindString(R.string.edit)
    String title;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.edit_value)
    EditText mEditValue;

    @Bind(R.id.edit_submit)
    TextView mEditSubmit;

    private String oldValue;
    private String type;


    public static void bundleIntent(Context context, String type, String value) {
        Bundle bundle = new Bundle();
        bundle.putString(EditUserInfoActivity.INTENT_TYPE, type);
        bundle.putString(EditUserInfoActivity.INTENT_VALUE, value);
        IntentUtils.startActivity(context, EditUserInfoActivity.class, bundle);

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_user_info;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        type = getIntent().getExtras().getString(INTENT_TYPE);
        if (INTENT_USER_NICK.equals(type)) {
            title = getString(R.string.edit_user_nick_text);
            mEditValue.setHint(R.string.nickname_text);
        } else if (INTENT_USER_INTRO.equals(type)) {
            title = getString(R.string.edit_user_intro_text);
            mEditValue.setHint(R.string.edit_user_intro_text);
            mEditValue.setLines(4);
            mEditValue.setGravity(Gravity.TOP|Gravity.LEFT);
        }
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {

        oldValue = getIntent().getExtras().getString(INTENT_VALUE);

        mEditValue.setText(oldValue);
        if (!TextUtils.isEmpty(oldValue))
            mEditValue.setSelection(oldValue.length());
        mEditValue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null && oldValue.equals(editable.toString())) {
                    mEditSubmit.setBackgroundResource(R.drawable.mine_login_shape);
                    mEditSubmit.setTextColor(getResources().getColor(R.color.gray));
                    mEditSubmit.setClickable(false);
                } else {
                    mEditSubmit.setBackgroundResource(R.drawable.mine_regis_shape);
                    mEditSubmit.setTextColor(getResources().getColor(R.color.white));
                    mEditSubmit.setClickable(true);
                }
            }
        });

    }

    @OnClick(R.id.edit_submit)
    public void submit(View view) {
        if (type.equals(INTENT_USER_NICK)) {
            requestTask();
        } else if (type.equals(INTENT_USER_INTRO)) {
            UserAPI.editUserInfo(null, mEditValue.getText().toString(), this, getLoading(-1));
        }
    }

    public void requestTask() {
        UserAPI.editUserInfo(mEditValue.getText().toString(), null, this, getLoading(-1));
    }

    @Override
    public void failed(String error) {
        super.failed(error);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        if (validateJson(json).isSuccess()) {
            if (this.type.equals(INTENT_USER_NICK)) {
                UserAPI.getCurrentUser().setUsername(mEditValue.getText().toString());
                EventBus.getDefault().post(new EventBusEntity(EventBusEntity.Type.USERNAME));
            } else if (this.type.equals(INTENT_USER_INTRO)) {
                UserAPI.getCurrentUser().setResume(mEditValue.getText().toString());
                EventBus.getDefault().post(new EventBusEntity(EventBusEntity.Type.USERDISC));
            }
            T.show(this, R.string.edit_success_text, Toast.LENGTH_SHORT);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}